home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / share / ewl / examples / ewl_image_test.c < prev    next >
C/C++ Source or Header  |  2006-01-09  |  9KB  |  292 lines

  1. #include "ewl_test.h"
  2.  
  3. static Ewl_Widget     *image_button;
  4. static Ewl_Widget     *image_win;
  5. static Ewl_Widget     *image_box;
  6. static Ewl_Widget     *image;
  7. static Ecore_DList    *images;
  8. static Ewl_Widget     *entry_path;
  9. static Ewl_Widget     *note_box;
  10. static Ewl_Widget     *note;
  11. static Ewl_Widget     *fd;
  12.  
  13. void __create_image_fd_cb(Ewl_Widget *w, void *ev_data, void *user_data);
  14.  
  15. static void __create_image_fd_window_response (Ewl_Widget *w, void *ev, void *data);
  16.  
  17. static void
  18. __destroy_image_test_window(Ewl_Widget * w, void *ev_data __UNUSED__,
  19.                         void *user_data __UNUSED__)
  20. {
  21.     char *str;
  22.  
  23.     ecore_dlist_goto_first(images);
  24.  
  25.     while ((str = ecore_dlist_remove_last(images)) != NULL)
  26.         FREE(str);
  27.  
  28.     ecore_dlist_destroy(images);
  29.     ewl_widget_destroy(w);
  30.     ewl_callback_append(image_button, EWL_CALLBACK_CLICKED,
  31.                 __create_image_test_window, NULL);
  32. }
  33.  
  34. static int
  35. __image_exists(char *i)
  36. {
  37.     struct stat st;
  38.  
  39.     if (!i || !strlen(i))
  40.         return -1;
  41.  
  42.     if (((stat(i, &st)) == -1) || !S_ISREG(st.st_mode))
  43.         return -1;
  44.  
  45.     return 1;
  46. }
  47.  
  48. static void
  49. __image_goto_prev_cb(Ewl_Widget * w __UNUSED__, void *ev_data __UNUSED__,
  50.                     void *user_data __UNUSED__)
  51. {
  52.     char *img = NULL;
  53.  
  54.     ecore_dlist_previous(images);
  55.     img = ecore_dlist_current(images);
  56.  
  57.     if (!img) img = ecore_dlist_goto_last(images);
  58.  
  59.     ewl_text_text_set(EWL_TEXT(entry_path), img);
  60.     ewl_image_file_set(EWL_IMAGE(image), img, NULL);
  61.  
  62.     ewl_widget_configure(image_win);
  63. }
  64.  
  65. static void
  66. __image_remove_cb(Ewl_Widget * w __UNUSED__, void *ev_data __UNUSED__,
  67.                     void *user_data __UNUSED__)
  68. {
  69.     char *img = NULL;
  70.  
  71.     img = ecore_dlist_remove(images);
  72.     if (img)
  73.         free(img);
  74.  
  75.     img = ecore_dlist_current(images);
  76.  
  77.     if (!img) img = ecore_dlist_goto_last(images);
  78.  
  79.     ewl_text_text_set(EWL_TEXT(entry_path), img);
  80.     ewl_image_file_set(EWL_IMAGE(image), img, NULL);
  81.  
  82.     ewl_widget_configure(image_win);
  83. }
  84.  
  85. static void
  86. __image_load()
  87. {
  88.     char *img = NULL;
  89.  
  90.     img = ewl_text_text_get(EWL_TEXT(entry_path));
  91.  
  92.     if (img && __image_exists(img)) {
  93.         ecore_dlist_append(images, img);
  94.         ecore_dlist_goto_last(images);
  95.         ewl_image_file_set(EWL_IMAGE(image), img, NULL);
  96.     } else
  97.         printf("ERROR: %s does not exist\n", img);
  98.  
  99.     ewl_widget_configure(image_win);
  100. }
  101.  
  102. static void
  103. __image_goto_next_cb(Ewl_Widget * w __UNUSED__, void *ev_data __UNUSED__,
  104.                     void *user_data __UNUSED__)
  105. {
  106.     char *img = NULL;
  107.  
  108.     ecore_dlist_next(images);
  109.     img = ecore_dlist_current(images);
  110.  
  111.     if (!img)
  112.         img = ecore_dlist_goto_first(images);
  113.  
  114.     ewl_text_text_set(EWL_TEXT(entry_path), img);
  115.     ewl_image_file_set(EWL_IMAGE(image), img, NULL);
  116.  
  117.     ewl_widget_configure(image_win);
  118. }
  119.  
  120. void
  121. __create_image_test_window(Ewl_Widget * w, void *ev_data __UNUSED__,
  122.                     void *user_data __UNUSED__)
  123. {
  124.     Ewl_Widget     *scrollpane;
  125.     Ewl_Widget     *button_hbox;
  126.     Ewl_Widget     *button_prev, *button_remove, *button_next;
  127.     char           *image_file = NULL;
  128.  
  129.     image_button = w;
  130.     images = ecore_dlist_new();
  131.  
  132.     image_win = ewl_window_new();
  133.     ewl_window_title_set(EWL_WINDOW(image_win), "Image Test");
  134.     ewl_window_name_set(EWL_WINDOW(image_win), "EWL Test Application");
  135.     ewl_window_class_set(EWL_WINDOW(image_win), "EFL Test Application");
  136.  
  137.     if (w) {
  138.         ewl_callback_del(w, EWL_CALLBACK_CLICKED, 
  139.                         __create_image_test_window);
  140.         ewl_callback_append(image_win, EWL_CALLBACK_DELETE_WINDOW,
  141.                         __destroy_image_test_window, NULL);
  142.     } else
  143.         ewl_callback_append(image_win, EWL_CALLBACK_DELETE_WINDOW,
  144.                         __close_main_window, NULL);
  145.     ewl_widget_show(image_win);
  146.  
  147.     /*
  148.      * Create the main box for holding the widgets
  149.      */
  150.     image_box = ewl_vbox_new();
  151.     ewl_container_child_append(EWL_CONTAINER(image_win), image_box);
  152.     ewl_box_spacing_set(EWL_BOX(image_box), 10);
  153.     ewl_widget_show(image_box);
  154.  
  155.     scrollpane = ewl_scrollpane_new();
  156.     ewl_container_child_append(EWL_CONTAINER(image_box), scrollpane);
  157.     ewl_widget_show(scrollpane);
  158.  
  159.     if ((__image_exists(PACKAGE_DATA_DIR "/images/e-logo.png")) != -1)
  160.         image_file = strdup(PACKAGE_DATA_DIR "/images/e-logo.png");
  161.     else if ((__image_exists(PACKAGE_SOURCE_DIR "/data/images/e-logo.png")) != -1)
  162.         image_file = strdup(PACKAGE_SOURCE_DIR "/data/images/e-logo.png");
  163.     else if ((__image_exists("./data/images/e-logo.png")) != -1)
  164.         image_file = strdup("./data/images/e-logo.png");
  165.     else if ((__image_exists("../data/images/e-logo.png")) != -1)
  166.         image_file = strdup("../data/images/e-logo.png");
  167.  
  168.     image = ewl_image_new();
  169.     ewl_image_file_set(EWL_IMAGE(image), image_file, NULL);
  170.     ewl_object_padding_set(EWL_OBJECT(image), 0, 0, 5, 0);
  171.     ewl_object_alignment_set(EWL_OBJECT(image), EWL_FLAG_ALIGN_CENTER);
  172.     ewl_container_child_append(EWL_CONTAINER(scrollpane), image);
  173.     ewl_widget_show(image);
  174.  
  175.     if (image_file)
  176.         ecore_dlist_append(images, image_file);
  177.  
  178.     button_hbox = ewl_hbox_new();
  179.     ewl_box_spacing_set(EWL_BOX(button_hbox), 5);
  180.     ewl_object_fill_policy_set(EWL_OBJECT(button_hbox),
  181.                    EWL_FLAG_FILL_HFILL | EWL_FLAG_FILL_HSHRINK);
  182.     ewl_container_child_append(EWL_CONTAINER(image_box), button_hbox);
  183.     ewl_widget_show(button_hbox);
  184.  
  185.     entry_path = ewl_entry_new();
  186.     ewl_text_text_set(EWL_TEXT(entry_path), image_file);
  187.     ewl_object_fill_policy_set(EWL_OBJECT(entry_path),
  188.                    EWL_FLAG_FILL_HFILL | EWL_FLAG_FILL_HSHRINK);
  189.     ewl_object_alignment_set(EWL_OBJECT(entry_path), EWL_FLAG_ALIGN_CENTER);
  190.     ewl_container_child_append(EWL_CONTAINER(button_hbox), entry_path);
  191.     ewl_widget_show(entry_path);
  192.  
  193.     button_remove = ewl_button_new();
  194.     ewl_button_label_set(EWL_BUTTON(button_remove), "Browse...");
  195.     ewl_callback_append(button_remove, EWL_CALLBACK_CLICKED,
  196.                 __create_image_fd_cb, entry_path);
  197.     ewl_object_fill_policy_set(EWL_OBJECT(button_remove), EWL_FLAG_FILL_NONE);
  198.     ewl_object_alignment_set(EWL_OBJECT(button_remove),
  199.                  EWL_FLAG_ALIGN_CENTER);
  200.     ewl_container_child_append(EWL_CONTAINER(button_hbox), button_remove);
  201.     ewl_widget_show(button_remove);
  202.  
  203.     button_hbox = ewl_hbox_new();
  204.     ewl_box_spacing_set(EWL_BOX(button_hbox), 5);
  205.     ewl_object_fill_policy_set(EWL_OBJECT(button_hbox),
  206.                    EWL_FLAG_FILL_HFILL);
  207.     ewl_object_alignment_set(EWL_OBJECT(button_hbox),
  208.                  EWL_FLAG_ALIGN_CENTER);
  209.     ewl_container_child_append(EWL_CONTAINER(image_box), button_hbox);
  210.     ewl_widget_show(button_hbox);
  211.  
  212.     button_prev = ewl_button_new();
  213.     ewl_button_label_set(EWL_BUTTON(button_prev), "Previous");
  214.     button_remove = ewl_button_new();
  215.     ewl_button_label_set(EWL_BUTTON(button_remove), "Remove");
  216.     button_next = ewl_button_new();
  217.     ewl_button_label_set(EWL_BUTTON(button_next), "Next");
  218.  
  219.     ewl_container_child_append(EWL_CONTAINER(button_hbox), button_prev);
  220.     ewl_container_child_append(EWL_CONTAINER(button_hbox), button_remove);
  221.     ewl_container_child_append(EWL_CONTAINER(button_hbox), button_next);
  222.  
  223.     ewl_callback_append(button_prev, EWL_CALLBACK_CLICKED,
  224.                 __image_goto_prev_cb, NULL);
  225.     ewl_callback_append(button_remove, EWL_CALLBACK_CLICKED,
  226.                 __image_remove_cb, NULL);
  227.     ewl_callback_append(button_next, EWL_CALLBACK_CLICKED,
  228.                 __image_goto_next_cb, NULL);
  229.  
  230.     ewl_widget_show(button_prev);
  231.     ewl_widget_show(button_remove);
  232.     ewl_widget_show(button_next);
  233.  
  234.  
  235.     note_box = ewl_hbox_new();
  236.     ewl_container_child_append(EWL_CONTAINER(image_box), note_box);
  237.     ewl_object_alignment_set(EWL_OBJECT(note_box), EWL_FLAG_ALIGN_CENTER);
  238.     ewl_object_maximum_h_set(EWL_OBJECT(note_box), 20);
  239.     ewl_widget_show(note_box);
  240.  
  241.     note = ewl_text_new();
  242.     ewl_text_text_set(EWL_TEXT(note), "Simple image viewer, load up images and page through them.");
  243.     ewl_container_child_append(EWL_CONTAINER(note_box), note);
  244.     ewl_widget_show(note);
  245.  
  246.         ewl_widget_show(image);
  247. }
  248.  
  249. void
  250. __create_image_fd_cb(Ewl_Widget *w __UNUSED__, void *ev_data __UNUSED__,
  251.                     void *user_data)
  252. {
  253.     if (fd)
  254.         return;
  255.  
  256.     fd = ewl_filedialog_new();
  257.     ewl_window_title_set (EWL_WINDOW (fd), "Select an Image...");
  258.     ewl_window_name_set (EWL_WINDOW (fd), "EWL Image Test");
  259.     ewl_window_class_set (EWL_WINDOW (fd), "EWL Filedialog");
  260.     ewl_filedialog_type_set(EWL_FILEDIALOG(fd), EWL_FILEDIALOG_TYPE_OPEN);
  261.     ewl_callback_append (fd, EWL_CALLBACK_VALUE_CHANGED, 
  262.                 __create_image_fd_window_response, user_data);
  263.     ewl_widget_show(fd);
  264. }
  265.  
  266. static void
  267. __create_image_fd_window_response (Ewl_Widget *w, void *ev, void *data)
  268. {
  269.     Ewl_Dialog_Event *e;
  270.     Ewl_Widget *entry = data;
  271.   
  272.     e = ev;
  273.  
  274.     if (e->response == EWL_STOCK_OPEN) {
  275.         char *path;
  276.  
  277.         printf("File open from image test: %s\n", 
  278.         path = ewl_filedialog_file_get (EWL_FILEDIALOG (w)));
  279.         if (path) {
  280.             ewl_text_text_set(EWL_TEXT(entry), path);
  281.             __image_load();
  282.             // FREE(path); FIXME: Is text widget allocated correctly?
  283.         }
  284.     }
  285.     else {
  286.         printf("Test program says bugger off.\n");
  287.     }
  288.  
  289.     ewl_widget_destroy(fd);
  290.     fd = NULL;
  291. }
  292.